package com.ruoyi.inventory.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.inventory.domain.InventoryTransaction;
import com.ruoyi.inventory.service.IInventoryTransactionService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 库存流水Controller
 * 
 * @author ruoyi
 * @date 2025-06-23
 */
@Controller
@RequestMapping("/inventory/transaction")
public class InventoryTransactionController extends BaseController
{
    private String prefix = "inventory/transaction";

    @Autowired
    private IInventoryTransactionService inventoryTransactionService;

    @RequiresPermissions("inventory:transaction:view")
    @GetMapping()
    public String transaction()
    {
        return prefix + "/transaction";
    }

    /**
     * 查询库存流水列表
     */
    @RequiresPermissions("inventory:transaction:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(InventoryTransaction inventoryTransaction)
    {
        startPage();
        List<InventoryTransaction> list = inventoryTransactionService.selectInventoryTransactionList(inventoryTransaction);
        return getDataTable(list);
    }

    /**
     * 导出库存流水列表
     */
    @RequiresPermissions("inventory:transaction:export")
    @Log(title = "库存流水", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(InventoryTransaction inventoryTransaction)
    {
        List<InventoryTransaction> list = inventoryTransactionService.selectInventoryTransactionList(inventoryTransaction);
        ExcelUtil<InventoryTransaction> util = new ExcelUtil<InventoryTransaction>(InventoryTransaction.class);
        return util.exportExcel(list, "库存流水数据");
    }

    /**
     * 新增库存流水
     */
    @RequiresPermissions("inventory:transaction:add")
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存库存流水
     */
    @RequiresPermissions("inventory:transaction:add")
    @Log(title = "库存流水", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(InventoryTransaction inventoryTransaction)
    {
        return toAjax(inventoryTransactionService.insertInventoryTransaction(inventoryTransaction));
    }

    /**
     * 修改库存流水
     */
    @RequiresPermissions("inventory:transaction:edit")
    @GetMapping("/edit/{transactionId}")
    public String edit(@PathVariable("transactionId") Long transactionId, ModelMap mmap)
    {
        InventoryTransaction inventoryTransaction = inventoryTransactionService.selectInventoryTransactionByTransactionId(transactionId);
        mmap.put("inventoryTransaction", inventoryTransaction);
        return prefix + "/edit";
    }

    /**
     * 修改保存库存流水
     */
    @RequiresPermissions("inventory:transaction:edit")
    @Log(title = "库存流水", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(InventoryTransaction inventoryTransaction)
    {
        return toAjax(inventoryTransactionService.updateInventoryTransaction(inventoryTransaction));
    }

    /**
     * 删除库存流水
     */
    @RequiresPermissions("inventory:transaction:remove")
    @Log(title = "库存流水", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(inventoryTransactionService.deleteInventoryTransactionByTransactionIds(ids));
    }
}
